﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace Luminal
{
    public class LuminalObject : IDisposable
    {
        public IntPtr InternalPointer { get; private set; }

        public bool Disposed { get; protected set; }

        ~LuminalObject()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            if (Disposed)
                throw new ObjectDisposedException(GetType().FullName);

            try
            {
                Dispose(true);
            }
            finally
            {
                ObjectTracker.UnregisterObject(this);
                InternalPointer = IntPtr.Zero;
                Disposed = true;

                GC.SuppressFinalize(this);
            }
        }

        protected void Initialize(IntPtr internalPtr)
        {
            InternalPointer = internalPtr;
            ObjectTracker.RegisterObject(this);
        }

        protected virtual void Dispose(bool disposing)
        {

        }
    }
}
